package jvm;

import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Europa
 * @create 2020-05-08 13:32
 */
public class RunDemo implements Runnable {

    public volatile int i;
    public volatile int j;
    public AtomicInteger atomicInteger = new AtomicInteger();
    Lock lock = new ReentrantLock();
    @Override
    public void run() {
        add();
        lockAdd();
        atomicAdd();
    }

    public void add() {
        for (int j = 0; j < 1000; j++) {
            this.i++;
        }
    }

    public void lockAdd() {
        lock.lock();
        try {
            for (int j = 0; j < 1000; j++) {
                this.j++;
            }
        } finally {
            lock.unlock();
        }
    }

    public void atomicAdd() {
        for (int k = 0; k < 1000; k++) {
            atomicInteger.getAndIncrement();
        }
    }



}
